package com.simple.component;

import android.text.TextUtils;

import com.billy.cc.core.component.CC;
import com.billy.cc.core.component.CCResult;
import com.billy.cc.core.component.IComponentCallback;

import java.util.Map;

/**
 * Copyright (C), 2016-2020,呼我出行网络科技有限公司
 * FileName: CcCall
 * Author: Administrator
 * Date: 2020/11/3 17:46
 * Description:
 * History:
 */
public class CcCall {
    public CcCall() {
    }

    public static CC.Builder newBuilder(String componentName, String actionName) {
        return CC.obtainBuilder(componentName).setActionName(actionName);
    }

    public static CCResult callComponent(String componentName, String actionName) {
        return callComponent(componentName, actionName, (String) null, (Object) null);
    }

    public static CCResult callComponent(String componentName, String actionName, Object... args) {
        CC.Builder builder = newBuilder(componentName, actionName);
        setParam(builder, args);
        return builder.build().call();
    }

    public static CCResult callComponent(String componentName, String actionName, String extraKey, Object extraValue) {
        CC.Builder builder = newBuilder(componentName, actionName);
        if (!TextUtils.isEmpty(extraKey)) {
            builder.addParam(extraKey, extraValue);
        }

        return builder.build().call();
    }

    public static CCResult callComponent(String componentName, String actionName, Map<String, Object> params) {
        CC.Builder builder = newBuilder(componentName, actionName);
        if (params != null && !params.isEmpty()) {
            builder.setParams(params);
        }

        return builder.build().call();
    }

    public static String callComponentAsync(String componentName, String actionName) {
        return callComponentAsync(componentName, actionName);
    }

    public static String callComponentAsync(String componentName, String actionName, IComponentCallback callback) {
        CC.Builder builder = newBuilder(componentName, actionName);
        return builder.build().callAsync(callback);

    }

    public static String callComponentAsync(String componentName, String actionName, Object... args) {
        return callComponentAsyncMain(componentName, actionName, false, (IComponentCallback) null, args);
    }

    public static String callComponentAsyncMain(String componentName, String actionName, boolean immediatelyReturn, IComponentCallback callback, Object... args) {
        CC.Builder builder = newBuilder(componentName, actionName);
        setParam(builder, args);
        builder.addParam("return_time", immediatelyReturn);
        return callback == null ? builder.build().callAsync() : builder.build().callAsyncCallbackOnMainThread(callback);
    }

    public static void setParam(CC.Builder builder, Object... args) {
        if (args != null && args.length > 0) {
            int length = args.length;

            for (int i = 0; i < length; ++i) {
                if (i % 2 == 0 && i + 1 < length) {
                    Object key = args[i];
                    if (key instanceof String) {
                        builder.addParam((String) key, args[i + 1]);
                    }
                }
            }
        }

    }
}
